package cn.edu.neusoft.yuejian4.yuejian_4.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Train;

/**
 * Created by Lix on 2018/10/24.
 */

public class MyCollectionTrainAdapter extends RecyclerView.Adapter<MyCollectionTrainAdapter.ViewHolder>{

    private List<Train> mTrain;
    private Context mContext;
    mOnItemClickListener mOnItemClickListener;

    public MyCollectionTrainAdapter(Context mContext, List<Train> mTrain){
        this.mContext = mContext;
        this.mTrain = mTrain;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyCollectionTrainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_my_cardview ,parent,false);
        MyCollectionTrainAdapter.ViewHolder holder = new MyCollectionTrainAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyCollectionTrainAdapter.ViewHolder holder, final int position) {
        final Train train = mTrain.get(position);
        holder.trian_name.setText(train.getTrain_name());
        holder.train_address.setText(train.getAddress());
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        };

        holder.btn_unCollection.setOnClickListener(onClickListener);
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+train.getPic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.train_img);

    }

    @Override
    public int getItemCount() {
        return mTrain.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView train_img;
        TextView trian_name;
        TextView train_address;
        Button btn_unCollection;


        public ViewHolder(View view){
            super(view);
            train_img = (ImageView)view.findViewById(R.id.collection_train_img);
            trian_name = (TextView)view.findViewById(R.id.like_club_namcollection_train_name);
            train_address = (TextView)view.findViewById(R.id.collection_train_address);
            btn_unCollection = (Button)view.findViewById(R.id.uncollection_trian_btn);

        }


    }

    public interface mOnItemClickListener {
        void onClick(View view, int position);
    }

    public void setmOnItemClickListener(MyCollectionTrainAdapter.mOnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
